package com.fang.async;

import org.apache.commons.lang.RandomStringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.async.DeferredResult;

@RestController
public class AsyncController {

	private Logger logger = LoggerFactory.getLogger(AsyncController.class);

	@Autowired
	private MockQueue mockQueue;

	@Autowired
	private DeferredResultHolder deferredResultHolder;

	/**
	 * 副线成再
	 */
	@GetMapping("order")
	public DeferredResult<String> order() {
		logger.info("tomcat主线程开始");
		/*
		 * Callable<String> result = new Callable<String>() {
		 * 
		 * @Override public String call() throws Exception {
		 * logger.info("副线程开始"); Thread.sleep(1000); logger.info("副线成结束");
		 * return "success"; } };
		 */

		// 这个订单号对应的处理信息
		String orderNumber = RandomStringUtils.randomNumeric(8);
		mockQueue.setStartOrder(orderNumber);
		DeferredResult<String> result = new DeferredResult<>();
		deferredResultHolder.getMap().put(orderNumber, result);
		logger.info("主线程结束");
		return result;
	}

}
